From eca9994181dc355cfba61e726c038832d5ff6975 Mon Sep 17 00:00:00 2001
From: Daniel Kolesa <daniel@octaforge.org>
Date: Sun, 23 Oct 2022 00:47:57 +0200
Subject: [PATCH 1/4] clang: add fortify include paths for musl triplets

When we have fortify headers this lets them get included.
---
 clang/lib/Driver/ToolChains/Linux.cpp | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Linux.cpp b/clang/lib/Driver/ToolChains/Linux.cpp
index 7fcdcf125..d5df4a240 100644
--- a/clang/lib/Driver/ToolChains/Linux.cpp
+++ b/clang/lib/Driver/ToolChains/Linux.cpp
@@ -622,10 +622,14 @@ void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
   if (getTriple().getOS() == llvm::Triple::RTEMS)
     return;
 
-  // Add an include of '/include' directly. This isn't provided by default by
-  // system GCCs, but is often used with cross-compiling GCCs, and harmless to
-  // add even when Clang is acting as-if it were a system compiler.
-  addExternCSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/include"));
+  if (getTriple().isMusl()) {
+    addExternCSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/usr/include/fortify"));
+  } else {
+    // Preserve original clang behavior for non-musl triples, but for musl
+    // this just gets in the way and none of our crosstoolchains are ever
+    // actually built this way (and we'd have to figure out fortify for it)
+    addExternCSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/include"));
+  }
 
   addExternCSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/usr/include"));
 
-- 
2.38.0

